package com.parenting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.parenting.entities.*;
import com.parenting.enums.ResultEnum;
import com.parenting.exception.BaseException;
import com.parenting.mapper.ActivityMapper;
import com.parenting.model.IMPage;
import com.parenting.model.UserDTO;
import com.parenting.request.*;
import com.parenting.request.wechat.ActivityListConditionRequest;
import com.parenting.response.CollageResponse;
import com.parenting.response.FissionResponse;
import com.parenting.response.FreeResponse;
import com.parenting.response.VoucherResponse;
import com.parenting.response.patform.ActivityPageListResponse;
import com.parenting.response.wechat.WechatActivityListResponse;
import com.parenting.response.wechat.WechatActivityNoticeResponse;
import com.parenting.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parenting.util.BeansListUtil;
import com.parenting.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 活动 服务实现类
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-08-27
 */
@Service
@Slf4j
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, ActivityDO> implements ActivityService {
    /**
     *   普通拼
     */
    @Autowired
    private ActivityCollageService activityCollageService;
    //裂变0元购
    @Autowired
    private ActivityFissionService activityFissionService;
    //免费体验
    @Autowired
    private ActivityFreeService activityFreeService;
    //代金优惠券
    @Autowired
    private ActivityVoucherService activityVoucherService;

    @Autowired
    private  CouponService  couponService;

    @Autowired
    private CommodityService  commodityService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addFissionActivity(AddActivityFissionRequest fissionRequest, UserDTO userInfo) {
        ActivityDO activityDO = BeanUtil.copyProperties(fissionRequest, ActivityDO.class);
        activityDO.setShopId(userInfo.getShopId());
        activityDO.setShopName(userInfo.getShopName());
        activityDO.setCreatedBy(userInfo.getFullName());
        activityDO.setActivityState(DateUtils.isStaticDate(new Date(),fissionRequest.getActivityStartTime(),fissionRequest.getActivityEndTime()));
        baseMapper.insert(activityDO);
        List<ActivityFissionDO> activityFissionDOS = BeansListUtil.listCopy(fissionRequest.getFission(), ActivityFissionDO.class);
        if (null!= activityFissionDOS && !activityFissionDOS.isEmpty()){
            for (ActivityFissionDO a:activityFissionDOS) {
                a.setShopId(userInfo.getShopId());
                a.setActivityId(activityDO.getActivityId());
            }
            activityFissionService.saveBatch(activityFissionDOS);
            couponService.updateNumberOfCouponCouponById(activityFissionDOS.get(0).getGroupNumber(),activityFissionDOS.get(0).getCouponId());
        }else{
            throw  new BaseException(ResultEnum.ACTIVITY_CONTENT );
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addFreeActivity(AddActivityFreeRequest freeRequest,  UserDTO userInfo) {
        ActivityDO activityDO = BeanUtil.copyProperties(freeRequest, ActivityDO.class);
        activityDO.setShopId(userInfo.getShopId());
        activityDO.setShopName(userInfo.getShopName());
        activityDO.setCreatedBy(userInfo.getFullName());
        activityDO.setActivityState(DateUtils.isStaticDate(new Date(),freeRequest.getActivityStartTime(),freeRequest.getActivityEndTime()));
        baseMapper.insert(activityDO);
        List<ActivityFreeDO> ActivityFreeDOS = BeansListUtil.listCopy(freeRequest.getFree(), ActivityFreeDO.class);
        if (null!= ActivityFreeDOS && !ActivityFreeDOS.isEmpty()){
            for (ActivityFreeDO a:ActivityFreeDOS) {
                a.setShopId(userInfo.getShopId());
                a.setActivityId(activityDO.getActivityId());
            }
            activityFreeService.saveBatch(ActivityFreeDOS);
            commodityService.updateStockBy(ActivityFreeDOS.get(0).getCommodityId(),ActivityFreeDOS.get(0).getCommodityNumber());
        }else{
            throw  new BaseException(ResultEnum.ACTIVITY_CONTENT );
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addVoucherActivity(AddActivityVoucherRequest voucherRequest, UserDTO userInfo) {
        ActivityDO activityDO = BeanUtil.copyProperties(voucherRequest, ActivityDO.class);
        activityDO.setShopId(userInfo.getShopId());
        activityDO.setShopName(userInfo.getShopName());
        activityDO.setCreatedBy(userInfo.getFullName());
        activityDO.setActivityState(DateUtils.isStaticDate(new Date(),voucherRequest.getActivityStartTime(),voucherRequest.getActivityEndTime()));
        baseMapper.insert(activityDO);
        List<ActivityVoucherDO> activityVoucherDOs = BeansListUtil.listCopy(voucherRequest.getVoucher(), ActivityVoucherDO.class);
        if (null!= activityVoucherDOs && !activityVoucherDOs.isEmpty()){
            for (ActivityVoucherDO a:activityVoucherDOs) {
                a.setShopId(userInfo.getShopId());
                a.setActivityId(activityDO.getActivityId());
            }
            activityVoucherService.saveBatch(activityVoucherDOs);
            couponService.updateNumberOfCouponCouponById(activityVoucherDOs.get(0).getCouponNumber(),activityVoucherDOs.get(0).getCouponId());
        }else{
            throw  new BaseException(ResultEnum.ACTIVITY_CONTENT );
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addCollageActivity(AddActivityCollageRequest request,  UserDTO userInfo) {
        ActivityDO activityDO = BeanUtil.copyProperties(request, ActivityDO.class);
        activityDO.setShopId(userInfo.getShopId());
        activityDO.setShopName(userInfo.getShopName());
        activityDO.setCreatedBy(userInfo.getFullName());
        activityDO.setActivityState(DateUtils.isStaticDate(new Date(),request.getActivityStartTime(),request.getActivityEndTime()));
        baseMapper.insert(activityDO);
        List<ActivityCollageDO> activityCollageDOS = BeansListUtil.listCopy(request.getCollageRequests(), ActivityCollageDO.class);
        if (null!= activityCollageDOS && !activityCollageDOS.isEmpty()){
            for (ActivityCollageDO a:activityCollageDOS) {
                a.setShopId(userInfo.getShopId());
                a.setActivityId(activityDO.getActivityId());
            }
            activityCollageService.saveBatch(activityCollageDOS);
            commodityService.updateStockBy(activityCollageDOS.get(0).getCommodityId(),activityCollageDOS.get(0).getCommodityNumber());
        }else{
            throw  new BaseException(ResultEnum.ACTIVITY_CONTENT );
        }
    }

    @Override
    public List<FissionResponse> getFissionActivity(String activityId) {
        List<FissionResponse> fissionResponses = BeansListUtil.listCopy(activityFissionService.
                getBaseMapper().selectList(new LambdaQueryWrapper<ActivityFissionDO>().eq(ActivityFissionDO::getActivityId,
                activityId)), FissionResponse.class);

        return fissionResponses;
    }

    @Override
    public List<FreeResponse> getFreeActivity(String activityId) {
        return BeansListUtil.listCopy(activityFreeService.getBaseMapper()
                .selectList(new LambdaQueryWrapper<ActivityFreeDO>().eq(ActivityFreeDO::getActivityId,activityId)),FreeResponse.class);
    }


    @Override
    public List<VoucherResponse> getVoucherActivity(String activityId) {
        return BeansListUtil.listCopy(activityVoucherService.getBaseMapper().selectList(
                new LambdaQueryWrapper<ActivityVoucherDO>().eq(ActivityVoucherDO::getActivityId,activityId)),VoucherResponse.class);
    }

    @Override
    public   List<CollageResponse>  getCollageActivity(String activityId) {
        List<ActivityCollageDO> activityCollageDOS = activityCollageService.getBaseMapper().selectList(
                new LambdaQueryWrapper<ActivityCollageDO>().eq(ActivityCollageDO::getActivityId, activityId));
        List<CollageResponse> collageResponses = BeansListUtil.listCopy(activityCollageDOS, CollageResponse.class);
        return  collageResponses;
    }

    @Override
    public IPage<ActivityDO> getActivityPageBy(ActivityBaseRequest activityBaseRequest,String ShopId) {
        LambdaQueryWrapper<ActivityDO> wrapper=new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(activityBaseRequest.getActivityName())){
            wrapper.like(ActivityDO::getActivityName,activityBaseRequest.getActivityName());
        }
        if (StringUtils.isNotEmpty(activityBaseRequest.getActivityType())){
            wrapper.eq(ActivityDO::getActivityType,activityBaseRequest.getActivityType());
        }
        if (null != activityBaseRequest.getActivityStartTime()){
            wrapper.ge(ActivityDO::getActivityStartTime,activityBaseRequest.getActivityStartTime());
        }

        if (null != activityBaseRequest.getActivityStartTime()){
            wrapper.lt(ActivityDO::getActivityEndTime,activityBaseRequest.getActivityEndTime());
        }
            wrapper.eq(ActivityDO::getShopId,ShopId);
        return baseMapper.selectPage(activityBaseRequest.getPage(),wrapper);
    }

    @Override
    public void closeActivity(List<String> activityIds) {
        if (null!=activityIds&&activityIds.size()>0){
            for (String s:activityIds ) {
                LambdaUpdateWrapper<ActivityDO> wrapper=new LambdaUpdateWrapper();
                wrapper.eq(ActivityDO::getActivityId,s);
                wrapper.set(ActivityDO::getActivityState,3);
                baseMapper.update(null,wrapper);
            }
        }
    }

    @Override
    public IMPage<WechatActivityListResponse> getWechatActivityPageListByType(ActivityListConditionRequest activityBaseRequest,String shopId) {
        log.info("========shopId================= {}" ,shopId);
        LambdaQueryWrapper<ActivityDO>  wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ActivityDO::getActivityType,activityBaseRequest.getType());
        wrapper.eq(ActivityDO::getShopId,shopId);
        IPage<ActivityDO> activityDOIPage = baseMapper.selectPage(activityBaseRequest.getPage(), wrapper);
        log.info("activityDOIPage==" ,activityDOIPage);
        IMPage<WechatActivityListResponse>  imPage=new IMPage<>();
        if (null!=activityDOIPage){
            List<WechatActivityListResponse> list=new ArrayList<>();
            imPage.setSize(activityDOIPage.getSize());
            imPage.setCurrent(activityDOIPage.getCurrent());
            imPage.setTotal(activityDOIPage.getTotal());

            for (ActivityDO a:activityDOIPage.getRecords()) {
                log.info("=======imPage===============",activityDOIPage.getRecords().get(0).toString());
                list.add(BeanUtil.copyProperties(a,WechatActivityListResponse.class));
            }
            imPage.setRecords(list);
        }
        return imPage;
    }

    @Override
    public List<WechatActivityNoticeResponse> activityNotice(String shopId) {
        return baseMapper.activityNotice(shopId);
    }

    @Override
    public  IMPage<ActivityPageListResponse>   getActivityPlatformPageBy(ActivityBaseRequest activityBaseRequest) {
        LambdaQueryWrapper<ActivityDO> wrapper=new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(activityBaseRequest.getActivityName())){
            wrapper.like(ActivityDO::getActivityName,activityBaseRequest.getActivityName());
        }
        if (StringUtils.isNotEmpty(activityBaseRequest.getActivityType())){
            wrapper.eq(ActivityDO::getActivityType,activityBaseRequest.getActivityType());
        }
        if (null != activityBaseRequest.getActivityStartTime()){
            wrapper.ge(ActivityDO::getActivityStartTime,activityBaseRequest.getActivityStartTime());
        }

        if (null != activityBaseRequest.getActivityStartTime()){
            wrapper.lt(ActivityDO::getActivityEndTime,activityBaseRequest.getActivityEndTime());
        }

        if(StringUtils.isNotEmpty(activityBaseRequest.getShopId())){
            wrapper.eq(ActivityDO::getShopId,activityBaseRequest.getShopId());
        }
        IPage<ActivityDO> activityDOIPage = baseMapper.selectPage(activityBaseRequest.getPage(), wrapper);
        IMPage<ActivityPageListResponse> imPage=new IMPage<>();
        imPage.setSize(activityDOIPage.getSize());
        imPage.setCurrent(activityDOIPage.getCurrent());
        imPage.setTotal(activityDOIPage.getTotal());
        List<ActivityPageListResponse> listResponses=new ArrayList<>();
        if (null!=activityDOIPage.getRecords()&&activityDOIPage.getRecords().size()>0){
            for (ActivityDO activityDO:activityDOIPage.getRecords()){
                ActivityPageListResponse activityPageListResponse = BeanUtil.copyProperties(activityDO, ActivityPageListResponse.class);
                listResponses.add(activityPageListResponse);
            }
        }
        imPage.setRecords(listResponses);
        return imPage;
    }


}
